package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Consumption;
import cn.wolfcode.qo.ConsumptionQueryObject;
import cn.wolfcode.qo.UserContext;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionItemService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.util.ConsumptionStatusEnum;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {

    @Autowired
    private IConsumptionService consumptionService;

    @Autowired
    private IConsumptionItemService consumptionServiceItem;


    @Autowired
    private IBusinessService businessService;

    @RequiredPermission(name = "结算单页面", expression = "consumption:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") ConsumptionQueryObject qo) {
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);
        model.addAttribute("enums", ConsumptionStatusEnum.values());
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("businesses", businessService.selectBusinessList());
        return "consumption/list";
    }

    @RequestMapping("/delete")
    @RequiredPermission(name = "结算单删除", expression = "consumption:delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        if (id != null) {
            consumptionService.delete(id);
        }
        return new JsonResult();
    }

    @RequestMapping("/deleteChecked")
    @RequiredPermission(name = "结算明细删除", expression = "consumption:deleteChecked")
    @ResponseBody
    public JsonResult deleteChecked(Long[] arr) {
        if (arr != null) {
            consumptionServiceItem.deleteChecked(arr);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "结算单新增/编辑", expression = "consumption:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Consumption consumption) {
        if (consumption.getId() != null) {
            consumptionService.update(consumption);
        } else {
            consumptionService.save(consumption);
        }
        return new JsonResult();
    }


    @RequestMapping("/save")
    @RequiredPermission(name = "结算单客户已消费", expression = "consumption:save")
    @ResponseBody
    public JsonResult save(String appointmentAno) {
        Consumption consumption = consumptionService.save(appointmentAno);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(consumption);
        return jsonResult;
    }


    @RequestMapping("/input")
    @RequiredPermission(name = "结算单新增/编辑", expression = "consumption:saveOrUpdate")
    public String input(Long id, Model model) {
        //页面有门店的下拉框 所以我们要获取
        model.addAttribute("businesses", businessService.selectBusinessList());
        if (id != null) {
            //因为我们生成的是没有连表的 只有自己本身的字段 但是页面还有数据没显示
            //就是因为我们还没有根据外键查询数据 所以要去sql修改
            Consumption consumption = consumptionService.get(id);
            model.addAttribute("consumption", consumption);

            //根据结算单的流水号去查明细数据 回显到页面上
            model.addAttribute("items", consumptionServiceItem.selectByCno(consumption.getCno()));
        }
        return "consumption/input";
    }

    @RequestMapping("/updatePayeeMsg")
    @RequiredPermission(name = "结算单结算", expression = "consumption:updatePayeeMsg")
    @ResponseBody
    public JsonResult updatePayeeMsg(Long id) {
         consumptionService.updatePayeeMsg(id,ConsumptionStatusEnum.AUDIT.getValue(),
                 new Date(), UserContext.getEmployee().getId());
        return new JsonResult();
    }

    @RequestMapping("/updateAuditorMsg")
    @RequiredPermission(name = "结算单审核", expression = "consumption:updateAuditorMsg")
    @ResponseBody
    public JsonResult updateAuditorMsg(Long id) {
        consumptionService.updateAuditorMsg(id,ConsumptionStatusEnum.FINISH.getValue() ,
                new Date(), UserContext.getEmployee().getId());
        return new JsonResult();
    }

}
